
package org.utn.edu.tsb.parser_xml.parser;


import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Marcelo
 */
public class NodeXML extends DefaultMutableTreeNode implements Comparable {

    private String name, value;
    private List<NodeXML> nodes;
    private HashMap<String, String> properties;
    private static int newID;
    private int id;
    private boolean isComment;

    public NodeXML() {
        startUp();
    }

    public NodeXML(String name, String value) {
        this.name = name;
        this.value = value;
        this.isComment=false;
        startUp();
    }

    private void startUp() {
       List<NodeXML> aux = new LinkedList();
       setNodes(aux);
        properties = new HashMap<String, String>();
        id = ++newID;
    }

    @Override
    public int compareTo(Object o) {
        if (o.getClass().getCanonicalName().compareToIgnoreCase("Node") != 0) {
            return -1;
        } else {
            return id - ((NodeXML) o).getNodeID();
        }
    }

    public int getNodeID() {
        return id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * @return the nodes
     */
    public List<NodeXML> getNodes() {
        return nodes;
    }

    /**
     * @param nodes the nodes to set
     */
    public void setNodes(List<NodeXML> nodes) {
        this.nodes = nodes;
    }

    /**
     *
     * @param node
     */
    public void setNode(NodeXML node){
        this.nodes.add(node);
    }

    public void addNode(NodeXML node) {
        getNodes().add(node);
    }

    public void addPropertie(String name, String value) {
        properties.put(name, value);
    }

    /**
     * @return the properties
     */
    public HashMap<String, String> getProperties() {
        return properties;
    }

    public static void restartIDs()
    {
        newID = 0;
    }

    @Override
    public String toString() {
        if (null != name) {
            if (null != value) {
                return name + ": " + value;
            } else {
                return name;
            }
        } else {
            return "Undefined";
        }
    }

    public void setComment(){
        this.isComment = true;
    }

    public boolean getComment(){
        return isComment;
    }


}
